<template>
    <div class="order-report">
        <div class="report-table-top-title" style="height: 50px">
            商家权限
            <span style="color: #000;font-size: 15px;margin-left: 20px"></span>
            <span style="float: right">
                <el-button type="primary" plain size="small" @click="submit">确认保存</el-button>
                <el-button type="danger" plain size="small" @click="toMain">返回</el-button>
            </span>
        </div>
        <div class="order-report-tree">
            <el-tree
                class="order-report-tree"
                ref="tree"
                :props="props"
                :data="authList"
                :default-checked-keys="authListChecked"
                :default-expanded-keys="authListExpanded"
                show-checkbox
                node-key="id"
                @check-change="authChange">
            </el-tree>
        </div>
        
    </div>
</template>

<script type="text/ecmascript-6">
export default {
    data: function () {
        return {
            authList: [],
            hasAuthList: false,
            accessIds: '',
            props: {
              label: 'label',
              children: 'children'
            },
            isSubmit: false,
            timer: null
        }
    },
    mounted () {
    },
    created () {
        console.log('this.companyId')
        console.log(this.companyId, this.companyCode)
        this.getAuthority()
    },
    watch: {
    },
    components: {
    },
    props: {
    },
    computed: {
        companyId () {
            return this.$store.state.page1KTVAgentEdit
        },
        companyCode () {
            return this.$store.state.companyCode
        },
        authListChecked () {
            if (!this.hasAuthList) {
                return []
            }
            let result = []
            this.authList[0].children.map(item => {
                item.children.map(item2 => {
                    item2.children.map(item3 => {
                        if (item3.checked) {
                            result.push(item3.id)
                        }
                    })
                })
            })
            return result
        },
        authListExpanded () {
            // 默认展开
            if (!this.hasAuthList) {
                return []
            }
            let result = []
            this.authList[0].children.map(item => {
                result.push(item.id)
            })
            return result
        }
    },
    methods: {
        authChange (e) {
            // 改变权限
            let result = this.getChecked(this.authList)
            this.accessIds = result.join(',')
        },
        getAuthority () {
            let {timestamp, signature} = this.returnTimestampAndSignature()
            // 权限获取
            this.$http.post(this.$store.state.yjyIP + 'system_service/system/getCompanyFunction', {
                data: {
                    companyCode: this.companyCode
                },
                companycode: this.companyCode,
                timestamp: timestamp,
                signature: signature
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                response = response.body
                let result = []
                this.hasAuthList = true
                // 第一级
                response.data.map((item, index) => {
                    if (item.id === 0) {
                        result.push({
                            id: item.id,
                            label: item.name,
                            checked: !!item.checked,
                            children: []
                        })
                    }
                })
                // 第二级
                response.data.map((item, index) => {
                    if (item.pid === 0) {
                        result.map(access => {
                            if (item.pid === access.id) {
                                access.children.push({
                                    id: item.id,
                                    label: item.name,
                                    checked: !!item.checked,
                                    children: []
                                })
                            }
                        })
                    }
                })
                // 第三级
                result[0].children.map(item => {
                    response.data.map((res, index) => {
                        if (item.id === res.pid) {
                            item.children.push({
                                id: res.id,
                                label: res.name,
                                checked: !!res.checked,
                                children: []
                            })
                        }
                    })
                })
                // 第四级
                result[0].children.map(item => {
                    item.children.map(item2 => {
                        response.data.map((res, index) => {
                            if (item2.id === res.pid) {
                                item2.children.push({
                                    id: res.id,
                                    label: res.name,
                                    checked: !!res.checked
                                })
                            }
                        })
                    })
                })
                console.log(result)
                this.authList = result
            })
        },
        getChecked (arr) {
            // 收集 权限
            let tree = this.$refs.tree
            let keys = Array.from(tree.getCheckedKeys())
            let result = []
            for (let i = 0; i < arr.length; i++) {
                let item = arr[i]
                if (keys.indexOf(item.id) >= 0) {
                    // 判断是否选中
                    result.push(item.id)
                }
                if (item.children) {
                    let subResult = this.getChecked(item.children)
                    if (subResult.length > 0) {
                        result = result.concat(subResult)
                        // 加上 父级
                        result.push(item.id)
                    }
                }
            }
            // 去重
            let set = new Set(result)
            let newResult = Array.from(set)
            // 去 0
            let zeroIndex = newResult.indexOf(0)
            if (zeroIndex >= 0) {
                newResult.splice(zeroIndex, 1)
            }
            return newResult
        },
        submit () {
            if (this.isSubmit) {
                console.log('短时间内重复提交，阻止')
                if (!this.timer) {
                    this.timer = setTimeout(() => {
                        clearTimeout(this.timer)
                        this.timer = null
                        this.isSubmit = false
                    }, 10 * 1000)
                }
                return
            }
            this.isSubmit = true
            // 提交
            this.authChange()
            let {timestamp, signature} = this.returnTimestampAndSignature()
            // 权限获取
            this.$http.post(this.$store.state.yjyIP + '/system_service/system/updateCompanyFunction', {
                data: {
                    companyCode: this.companyCode,
                    function: this.accessIds
                },
                companycode: this.companyCode,
                timestamp: timestamp,
                signature: signature
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                this.isSubmit = false
                response = response.body
                if (response.code === '0') {
                    this.$message({
                        showClose: true,
                        message: '修改权限成功',
                        type: 'success'
                    })
                    this.toMain()
                } else {
                    this.$message('修改权限失败')
                }
            }, response => {
                this.isSubmit = false
                this.$message('修改权限失败')
            })
        },
        toMain () {
            this.$store.dispatch('setPageHeadShop', 0)
        }
    }
}
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.report-table-top-title
    padding-bottom 10px
.order-report-tree
    padding 20px 15px
</style>
